package io.github.junxworks.qt.modules.strategy.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import io.github.junxworks.ep.core.Result;
import io.github.junxworks.ep.core.utils.PageUtils;
import io.github.junxworks.ep.qlexp.CheckResult;
import io.github.junxworks.ep.qlexp.RuleEngine;
import io.github.junxworks.ep.sys.annotations.EpLog;
import io.github.junxworks.ep.sys.constants.RecordStatus;
import io.github.junxworks.qt.modules.strategy.dto.SStrategyCondition;
import io.github.junxworks.qt.modules.strategy.dto.SStrategyDto;
import io.github.junxworks.qt.modules.strategy.service.SStrategyService;
import io.github.junxworks.qt.modules.strategy.vo.SStrategyVo;

/**
 * s_strategy Entity Controller Class
 *
 * @since Oct 30, 2023, 10:57:37 AM Generated by JunxEP
 */
@RestController
@RequestMapping("/strategy/strategies")
public class SStrategyController {
	@Autowired
	private SStrategyService sStrategyService;

	@Autowired
	private RuleEngine ruleEngine;

	/**
	 * Query list.
	 *
	 * @param condition the condition
	 * @return the result
	 */
	@GetMapping()
	public Result querySStrategyList(SStrategyCondition condition) {
		// 查询列表数据
		PageUtils.setPage(condition);
		List<SStrategyVo> parents = sStrategyService.queryList(condition);
		if (condition.getQueryChildren()) {
			parents.forEach(p -> {
				condition.setParentId(p.getId());
				p.setChildren(sStrategyService.querySubStrategyList(condition));
			});
		}
		return Result.ok(new PageInfo<>(parents));
	}

	@GetMapping("/children")
	public Result querySubStrategyList(SStrategyCondition condition) {
		// 查询列表数据
		return Result.ok(new PageInfo<>(sStrategyService.querySubStrategyList(condition)));
	}

	/**
	 * Save .
	 *
	 * @param dto the dto
	 * @return the result
	 */
	@EpLog("策略-策略管理-保存策略")
	@PostMapping()
	public Result saveSStrategy(@RequestBody SStrategyDto dto) {
		sStrategyService.save(dto);
		return Result.ok();
	}

	/**
	 * Query entity by id.
	 *
	 * @param id the id
	 * @return the result
	 */
	@GetMapping("/{id}")
	public Result querySStrategyEntityById(@PathVariable("id") Long id) {
		return Result.ok(sStrategyService.querySStrategyById(id));
	}

	/**
	 * Delete.
	 *
	 * @param id the id
	 * @return the result
	 */
	@EpLog("策略-策略管理-删除策略")
	@DeleteMapping("/{id}")
	public Result deleteSStrategy(@PathVariable("id") Long id) {
		SStrategyDto dto = new SStrategyDto();
		dto.setId(id);
		dto.setStatus(RecordStatus.DELETED.getValue());
		sStrategyService.save(dto);
		return Result.ok();
	}

	@PostMapping("/compile")
	public Result compile(@RequestBody SStrategyDto dto) {
		CheckResult result = ruleEngine.checkExpression(dto.getContent());
		if (!result.isPass()) {
			return Result.error(result.getMessage() + ",异常原因：" + result.getExceptionCause());
		}
		return Result.ok();
	}
}